package com.wan.service.Provider;

import com.wan.dao.BaseDao;
import com.wan.dao.provider.ProviderDao;
import com.wan.dao.provider.ProviderDaoImpl;
import com.wan.pojo.Provider;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class ProviderServiceImpl implements ProviderService{

    private ProviderDao providerDao;
    public ProviderServiceImpl() {
        providerDao = new ProviderDaoImpl();
    }

    @Override
    public int proCount(String proCode, String proName) {

        Connection conn = null;
        int impactCount = 0;

        try{
            conn = BaseDao.getConnection();
            impactCount = providerDao.proCount(conn, proCode, proName);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return impactCount;
    }

    @Override
    public List<Provider> getProviderList(String proCode, String proName, int currentPageNo, int pageSize) {
        Connection conn = null;
        ArrayList<Provider> providerArrayList = new ArrayList<Provider>();

        try{
            conn = BaseDao.getConnection();
            providerArrayList = (ArrayList<Provider>) providerDao.getProviderList(conn,proCode, proName, currentPageNo, pageSize);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return providerArrayList;
    }

    @Override
    public int addProvider(Provider provider,int createdBy) {
        Connection conn = null;
        int impactCount = 0;

        try{
            conn = BaseDao.getConnection();
            impactCount = providerDao.addProvider(conn,provider,createdBy);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return impactCount;
    }

    @Override
    public int deleteProvider(int id) {
        Connection conn = null;
        int impactCount = 0;
        try{
            conn = BaseDao.getConnection();
            impactCount = providerDao.deleteProvider(conn,id);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return impactCount;
    }

    @Override
    public Provider getProviderById(int id) {
        Connection conn = null;
        Provider provider = null;
        try{
            conn = BaseDao.getConnection();
            provider = providerDao.getProviderById(conn,id);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return provider;
    }

    @Override
    public int updateProvider(Provider provider,int modifyBy) {
        Connection conn = null;
        int impactCount = 0;
        try{
            conn = BaseDao.getConnection();
            impactCount = providerDao.updateProvider(conn,provider,modifyBy);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return impactCount;
    }

    @Override
    public List<Provider> getProviderList_() {
        Connection conn = null;
        ArrayList<Provider> providerArrayList = null;
        try{
            conn = BaseDao.getConnection();
            providerArrayList = (ArrayList<Provider>) providerDao.getProviderList_(conn);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return providerArrayList;
    }
}
